/* tslint:disable */
import { Action, createSelector } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { BaseReducerFactory } from './base';
import { <%- modelName %>, <%- modelName %>Interface } from '../models';
import { LoopbackAction } from '../models/BaseModels';
import { <%- modelName %>ActionTypes } from '../actions';

export interface <%- modelName %>sState extends EntityState<<%- modelName %> | <%- modelName %>Interface> {};

export const <%- modelName %>Adapter: EntityAdapter<<%- modelName %> | <%- modelName %>Interface> = createEntityAdapter<<%- modelName %> | <%- modelName %>Interface>();

const initialState: <%- modelName %>sState = <%- modelName %>Adapter.getInitialState({});

const cases = BaseReducerFactory<<%- modelName %>sState, <%- modelName %> | <%- modelName %>Interface>(<%- modelName %>ActionTypes, <%- modelName %>Adapter);

/**
 * @module <%- modelName %>sReducer
 * @author João Ribeiro <@JonnyBGod> <github:JonnyBGod>
 * @license MIT
 * @description
 * Provides with a LoopBack compatible <%- modelName %> reducer.
 */
export function <%- modelName %>sReducer(state = initialState, action: LoopbackAction): <%- modelName %>sState {
  if (cases[action.type]) {
    return cases[action.type](state, action);
  } else {
    return state;
  }
}

export const get<%- modelName %>sState = (state: any) => state.<%- modelName %>s;
export const get<%- modelName %>sEntities = (state: any) => state.<%- modelName %>s.entities;
export const get<%- modelName %>sIds = (state: any) => state.<%- modelName %>s.ids;

export const get<%- modelName %>s =
  createSelector(get<%- modelName %>sEntities, get<%- modelName %>sIds, (entities, ids) => ids.map((id) => entities[id]));

export function get<%- modelName %>ById(id: string) {
  return (state: any) => state.<%- modelName %>s.entities[id];
}

export function get<%- modelName %>sById(ids: string[]) {
  return createSelector(get<%- modelName %>sEntities, (entities) => ids.map((id) => entities[id]));
}
